/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 23:34:52
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.core.bean.BpJobBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;

/**
 * BpJobDao
 *
 */
public interface BpJobDao extends JpaRepository<BpJob, Long>, QuerydslPredicateExecutor<BpJob> {
    public Page<BpJob> findAll(Specification<BpJob> spec, Pageable pageable);

    public BpJob save(BpJob bean);

//    public void delete(BpJob bean);

    @Query(value = "select * from  WWYT_BP_JOB where name = ?1 and delete_flag='0' and (show_state is null or show_state !=44) limit 1 ", nativeQuery = true)
    public BpJob findByName(String name);

    @Query(value = "select * from  WWYT_BP_JOB where name like concat('%',?1,'%') and delete_flag='0' and (show_state is null or show_state !=44)", nativeQuery = true)
    List<BpJob> findByNameLike(String name);

    @Query("select name from BpJob where id = ?1")
    public String findNameById(Long id);

    @Query("select name from BpJob where id = ?1")
    public String getNameById(Long jobId);

    @Transactional
    @Modifying
    @Query(value = "update BpJob set staffCount = (select count(*) from BaseUser where job = ?1) where id = ?1")
    void updateJobUserCount(Long jobId);

    @Query(value = "select count(id) from  WWYT_BP_JOB where name = ?1 and delete_flag='0' and (show_state is null or show_state !=44) ", nativeQuery = true)
    int countByName(String name);

    @Query(value = "select * from  WWYT_BP_JOB where tenent_id = ?1 and delete_flag='0' and (show_state is null or show_state !=44) ", nativeQuery = true)
    List<BpJob> findAllJobByTenentId(Long tenentId);

    @Query(value = "select * from  WWYT_BP_JOB where delete_flag='0' and (show_state is null or show_state !=44) ", nativeQuery = true)
    List<BpJob> findAll();

    @Query(value = "select * from  WWYT_BP_JOB where locate_sync = 1 and unique_id is not null and  delete_flag='0' and (show_state is null or show_state !=44) limit 1 ", nativeQuery = true)
    BpJob getSyncOne();

    @Query(value = "select * from  WWYT_BP_JOB where  unique_id = ?1 and  delete_flag='0' and (show_state is null or show_state !=44) limit 1 ", nativeQuery = true)
    BpJob findByUniqueId(String uniqueId);

    @Query(value = "select * from  WWYT_BP_JOB where name = ?1 and delete_flag='0' and (show_state is null or show_state !=44) AND TENENT_ID = ?2 AND ORG_CODE = ?3 limit 1 ", nativeQuery = true)
    BpJob findByNameAndCurrentCompany(String job, Long tenantId, String orgCode);

    @Query(value = "select distinct(j.id) from `wwyt_bp_job` j left join `wwyt_base_user` u on j.`ID` = u.`JOB` where j.`DELETE_FLAG` = '0' AND (j.show_state IS NULL OR j.show_state!=44) AND u.`id` in ?1",nativeQuery = true)
   List<Long> getJobIdsByUserIds(List<Long> userIds);

    @Query(value = "select distinct(j.id) from `wwyt_bp_job` j left join `wwyt_base_user` u on j.`ID` = u.`JOB` where j.`DELETE_FLAG` = '0' AND (j.show_state IS NULL OR j.show_state!=44) AND u.`depart_id` in ?1",nativeQuery = true)
    List<Long> getJobIdsByDepartIds(List<Long> deoartIds);

    @Query(value = "select * from  wwyt_bp_job where id in ?1 and delete_flag='0' and (show_state is null or show_state !=44)", nativeQuery = true)
    List<BpJob> findAllByIds(List<Long> parIds);

    @Query(value = "select * from  wwyt_bp_job where id in ?1  and delete_flag='0' and (show_state is null or show_state !=44)", nativeQuery = true)
    List<BpJob> findAllByIdInfo(List<Long> parIds);

    @Query(value = "select * from  wwyt_bp_job where id in ?1  and delete_flag='0' and (show_state is null or show_state !=44)", nativeQuery = true)
    List<BpJob> findbyIds(List<Long> jobIds);

    @Query(value = "select * from  wwyt_bp_job where id = ?1  and delete_flag='0' and (show_state is null or show_state !=44)", nativeQuery = true)
    BpJob findOneById(Long jobId);
}
